package com.syxbbs.forum.services.Impl;

import com.syxbbs.forum.Utils.StringUtils;
import com.syxbbs.forum.common.ResultCode;
import com.syxbbs.forum.dao.ArticleMapper;
import com.syxbbs.forum.exception.ApplicationException;
import com.syxbbs.forum.model.Article;
import com.syxbbs.forum.model.Board;
import com.syxbbs.forum.model.User;
import com.syxbbs.forum.services.IArticleService;
import com.syxbbs.forum.services.IBoardServive;
import com.syxbbs.forum.services.IUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {
    //不能出现循环引用的情况。
    @Resource
    ArticleMapper articleMapper;

    @Resource
    IBoardServive iBoardServive;

    @Resource
    IUserService iUserService;
    //插入文章
//    @Transactional
    @Override
    public void create(Article article) {
        //非空校验
        if(article==null || article.getUserId()==null ||
                StringUtils.isEmpty(article.getContent()) || StringUtils.isEmpty(article.getTitle()) ||
                article.getBoardId()==null){
            //给出警告，参数异常
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }
        //设置默认值
        article.setVisitCount(0);
        article.setLikeCount(0);
        article.setReplyCount(0);
        article.setDeleteState((byte) 0);
        article.setState((byte) 0);
        Date date=new Date();

        article.setUpdateTime(date);
        article.setCreateTime(date);
        //写入数据库
        int articlerow = articleMapper.insertSelective(article);
        if (articlerow<=0){
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(ResultCode.FAILED_CREATE.toString());

        }
        //获取用户信息
        User user=iUserService.selectById(article.getUserId());
        //没有找到指定用户信息
        if (user==null){
            log.warn(ResultCode.FAILED_CREATE.toString()+" 发帖失败 userId+"+article.getUserId());
            throw new ApplicationException(ResultCode.FAILED_CREATE.toString());
        }
        //更新用户发帖数进行加1
        iUserService.addOneArticleCountById(user.getId());
        //
        Board board = iBoardServive.    selectById(article.getBoardId());
        if (board==null){
            log.warn(ResultCode.FAILED_CREATE.toString()+" 发帖失败 board+"+article.getBoardId());
            throw new ApplicationException(ResultCode.FAILED_CREATE.toString());
        }
        //int a=10/0;
        iBoardServive.addOneArticleCountById(board.getId());
        log.info(ResultCode.SUCCESS.toString());




    }

    @Override
    public List<Article> selectAll() {
        return articleMapper.selectAll();
    }

    @Override
    public List<Article> selectAllByBoardId(Long boardId) {
        if (boardId==null || boardId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());

        }
        //查看板块是否存在
        Board board = iBoardServive.selectById(boardId);
        if (board==null){
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.toString());

        }

        List<Article> articles = articleMapper.selectAllByBoardId(boardId);
        return articles;


    }

    //点击查询界面
    @Override
    public Article selectDetailById(Long id) {
        if (id==null || id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());

        }
        Article article = articleMapper.selectDetailById(id);
        if (article==null){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
        }
        //更新帖子访问次数
        Article updateArticle=new Article();
        updateArticle.setId(article.getId());
        updateArticle.setVisitCount(article.getVisitCount()+1);
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if (row!=1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(ResultCode.ERROR_SERVICES.toString());
        }
        //数据库中已经返回成功但是这边的对象还没成功
        return articleMapper.selectDetailById(id);


    }

    @Override
    public void modify(Long id, String title, String content) {
        if (id==null || id<=0 || StringUtils.isEmpty(title) || StringUtils.isEmpty(content)){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());

        }
        Article article=new Article();
        article.setId(id);
        article.setTitle(title);
        article.setContent(content);
        //更新时间
        article.setUpdateTime(new Date());

        int row = articleMapper.updateByPrimaryKeySelective(article);
        if (row!=1){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }


    }

    @Override
    public Article selectById(Long id) {
        if (id==0 || id<=0 ){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }
        return articleMapper.selectByPrimaryKey(id);
    }

    @Override
    public void thumbUpById(Long id) {
        if (id==null || id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if (article==null || article.getDeleteState()==1){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());

        }
        if ( article.getState()==1 ){
            log.warn(ResultCode. FAILED_BOARD_BANNED.toString());
            throw new ApplicationException(ResultCode. FAILED_BOARD_BANNED.toString());

        }
        Article updateArticle=new Article();
        updateArticle.setId(id);
        updateArticle.setLikeCount(article.getLikeCount()+1);
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if (row!=1){
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            throw new ApplicationException(ResultCode.FAILED_ARTICLE_BANNED.toString());
        }


    }

    @Transactional
    @Override
    public void deleteById(Long id) {
        if (id==null || id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());

        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if (article==null || article.getDeleteState()==1){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
        }
        //更新文章将文章delete状态设置为1
        Article updateArticle=new Article();
        updateArticle.setId(article.getId());
        updateArticle.setDeleteState((byte) 1);
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if (row!=1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(ResultCode.ERROR_SERVICES.toString());
        }
        //更新板块数量
        iBoardServive.subOneArticleCountById(article.getBoardId());
        //更新用户文章数
        iUserService.subOneArticleCountById(article.getUserId());

    }

    @Transactional
    @Override
    public void addOneReplyCountById(Long id) {
        if (id==null || id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());

        }
        Article article=articleMapper.selectByPrimaryKey(id);
        //检验帖子状态
        if (article==null || article.getDeleteState()==1){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
        }
        //封贴
        if (article.getState()==1){
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            throw new ApplicationException(ResultCode.FAILED_ARTICLE_BANNED.toString());

        }
        Article updateArticle=new Article();
        updateArticle.setId(article.getId());
        updateArticle.setUpdateTime(new Date());
        updateArticle.setReplyCount(article.getReplyCount()+1);
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if (row!=1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(ResultCode.ERROR_SERVICES.toString());
        }


    }

    @Override
    public List<Article> selectByUserId(Long userId) {
        if (userId==null || userId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());

        }
        User user = iUserService.selectById(userId);
        if (user==null){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_USER_NOT_EXISTS.toString());
        }
        //调用dao开始查询
        List<Article> articles = articleMapper.selectByUserId(userId);
        return articles;

    }


}
